﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Net;
using System.Text;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace Usermanagment.VisualDisplay.UnderTheHood
{
	[Serializable()]
	public class ServerData : ISerializable
	{
		
		private String username, password, database,playersdb,statsdb,address;
		private Boolean firstRun = true;
		public ServerData()
		{ 
		}		
		public ServerData(String username, String password, String address, String database, String playersdb, String statsdb)
		{
			this.username = username;
			this.password = password;
			this.address = address;
			this.database = database;
			this.playersdb = playersdb;
			this.statsdb = statsdb;
		}
		public void GetObjectData(SerializationInfo info, StreamingContext context)
		{
			info.AddValue("Server:", address);
			info.AddValue("Username:", username);
			info.AddValue("Password:", password);
			info.AddValue("Database:", database);
			info.AddValue("PlayerTable:", playersdb);
			info.AddValue("PlayerStatTable:", statsdb);
			info.AddValue("FirstRun:",firstRun);
		}
		public ServerData(SerializationInfo info, StreamingContext context)
		{
			address = (String)info.GetValue("Server:", typeof(String));
			username = (String)info.GetValue("Username:", typeof(String));
			password = (String)info.GetValue("Password:", typeof(String));
			database = (String)info.GetValue("Database:", typeof(String));
			playersdb = (String)info.GetValue("PlayerTable:", typeof(String));
			statsdb = (String)info.GetValue("PlayerStatTable:", typeof(String));
			firstRun = (Boolean)info.GetValue("FirstRun:", typeof(Boolean));
		}
		public String sAddress
		{
			set
			{
				this.address = value;

			}

			get
			{
				return this.address;
			}
		}
		public String sUser
		{
			set
			{
				this.username = value;

			}

			get
			{
				return this.username;
			}
		}
		public String sPassword
		{
			set
			{
				this.password = value;

			}

			get
			{
				return this.password;
			}
		}
		public String sDatabase
		{
			set
			{
				this.database = value;

			}

			get
			{
				return this.database;
			}
		}
		public String sPlayersdb
		{
			set
			{
				this.playersdb = value;

			}

			get
			{
				return this.playersdb;
			}
		}
		public String sStatsdb
		{
			set
			{
				this.statsdb = value;

			}

			get
			{
				return this.statsdb;
			}
		}
		public Boolean isFirstRun
		{
			set
			{
				this.firstRun = value;
			}
			get
			{
				return this.firstRun;
			}
		}
		
	}

	public class SerializeDat
	{
		public void work(String username, String password, String address, String database, String playersdb, String statsdb)
		{
			ServerData sd = new ServerData(username,password,address,database,playersdb,statsdb);
			sd.isFirstRun = false;
			Stream stream = File.Open("server.cfg", FileMode.Create);
			BinaryFormatter bformat = new BinaryFormatter();
			bformat.Serialize(stream, sd);
			stream.Close();
		}

		public void work(ServerData sd)
		{
			if (sd != null)
			{
				sd.isFirstRun = false;
				Stream stream = File.Open("server.cfg", FileMode.Create);
				BinaryFormatter bformat = new BinaryFormatter();
				bformat.Serialize(stream, sd);
				stream.Close();
			}
		}

		public ServerData work()
		{
			ServerData sd = null;
			try
			{
				Stream stream = File.Open("server.cfg", FileMode.Open);
				BinaryFormatter bformat = new BinaryFormatter();
				sd = (ServerData)bformat.Deserialize(stream);
				stream.Close();
			}
			catch (Exception e)
			{
				Console.WriteLine(e.Message);
			}
			return sd;
		}
	}
}
